<?php

namespace RegisterUser\Model;

require_once 'DAL.php';

class RegisterUserModel {

	private $DAL;

	private $newUsername;
	private $newPassword;
	private $newPasswordRepeated;

	public $messageArray = array();

	const regexPattern = '^[<a-zA-Z]$';

	const minNameLeng = 3;
	const minPassLeng = 6;

	function __construct() {
		$this -> DAL = new DAL();
	}

	public function tryToRegister($newUsername, $newPassword, $newPasswordRepeated) {
		$this -> newUsername = $newUsername;
		$this -> newPassword = $newPassword;
		$this -> newPasswordRepeated = $newPasswordRepeated;
		if ($this -> checkInput()) {
			$this -> addUser();
			//TODO Lägg till meddelande om att användaren registrerats
			return TRUE;
		} else {
			return FALSE;
		}
	}

	private function checkInput() {
		return $this -> usernameNotOccupied() && $this -> allFieldsFilled() && $this -> validInput() && $this -> validLength() && $this -> passwordsMatch();
	}

	private function allFieldsFilled() {
		echo "2";
		if ($this -> newUsername != "" && $this -> newPassword != "" && $this -> newPasswordRepeated != "") {
			return TRUE;
		} else {
			if ($this -> newUsername == "") {
				$this -> messageArray[] = "Användarnamnet har för få tecken. Minst 3 tecken.";
			}
			if ($this -> newPassword == "") {
				$this -> messageArray[] = "Lösenordet har för få tecken. Minst 6 tecken.";
			}
			return FALSE;
		}
	}

	private function validInput() {
		echo "3";
		if (preg_match('/^[<a-zA-Z0-9]*$/', $this -> newUsername)) {
			return TRUE;
		} else {
			$this -> messageArray[] = "Användarnamnet innehåller ogiltiga tecken.";
			return FALSE;
		}
	}

	private function validLength() {
		echo "4";
		if (strlen($this -> newUsername) >= self::minNameLeng && strlen($this -> newPassword) >= self::minPassLeng) {
			return TRUE;
		} else {
			if (strlen($this -> newUsername) < self::minNameLeng) {
				$this -> messageArray[] = "Användarnamnet har för få tecken. Minst 3 tecken.";
			}
			if (strlen($this -> newPassword) < self::minPassLeng) {
				$this -> messageArray[] = "Lösenordet har för få tecken. Minst 6 tecken.";
			}
			return FALSE;
		}
	}

	private function usernameNotOccupied() {
		echo "1";
		$validUsername = TRUE;
		$userArray = $this -> DAL -> getUserArray();
		foreach($userArray as $i => $row){
			if($row[0] == $this -> newUsername){
				$validUsername = FALSE;
				$this -> messageArray[] = "Användarnamnet är redan upptaget.";
			}
		}		
		return $validUsername;
	}

	private function passwordsMatch() {
		echo "5";
		if ($this -> newPassword === $this -> newPasswordRepeated) {
			echo "dumkukar";
			return TRUE;
		} else {
			$this -> messageArray[] = "Lösenorden matchar inte.";
			echo "dumkuk";
			return FALSE;
		}
	}

	private function addUser() {
		$_SESSION['userDidRegister'] = TRUE;
		unset($_SESSION['doingRegistration']);
		$_SESSION['newlyRegisterdUser'] = $this -> newUsername;
		$this -> DAL -> insertUser($this -> newUsername ,$this -> newPassword);
	}
}